import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {

  state = { mouse: false }  //标记鼠标移入移除

  // 鼠标移入移除的回调
  handleMouse = (flag) => {
    return () => {
      this.setState({mouse: flag})
    }
  }

  // 勾选框勾选的回调
  hanleChange = (id) => {
    return (event) => {
      this.props.updateTodo(id,event.target.checked)
    }
  }

  // 删除item
  handleDelete = (id) => {
    return () => {
      if (window.confirm("确定删除吗")) {
        this.props.deletetodo(id)
      }
    }
    
  }
 
  render() {
    const { id, text, done } = this.props
    let mouse = this.state.mouse
    return (
      <li style={{backgroundColor: mouse?'#ddd':'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
          <label>
          <input type="checkbox" checked={done} onChange={this.hanleChange(id)} />
            <span>{text}</span>
          </label>
          <button className="btn btn-danger" style={{display:mouse?'block' :'none'}} onClick={this.handleDelete(id)} >删除</button>
        </li>
    )
  }
}
